package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Resource
    private GraphInfoService graphInfoService;
    @Resource
    private ApartmentFeeValueService apartmentFeeValueService;
    @Resource
    private ApartmentFacilityService apartmentFacilityService;
    @Resource
    private ApartmentLabelService apartmentLabelService;
    @Resource
    private RoomInfoMapper roomInfoMapper;
    @Resource
    private ApartmentInfoMapper apartmentInfoMapper;
    @Resource
    private ProvinceInfoMapper provinceInfoMapper;
    @Resource
    private CityInfoMapper cityInfoMapper;
    @Resource
    private DistrictInfoMapper districtInfoMapper;

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo vo) {
        boolean isUpdate = vo.getId() != null && vo.getId() != 0;
        //前端传输数据只有id 需要手动添加省市区的名称
            ProvinceInfo provinceInfo = provinceInfoMapper.selectById(vo.getProvinceId());
            CityInfo cityInfo = cityInfoMapper.selectById(vo.getCityId());
            DistrictInfo districtInfo = districtInfoMapper.selectById(vo.getDistrictId());
            vo.setProvinceName(provinceInfo.getName());
            vo.setCityName(cityInfo.getName());
            vo.setDistrictName(districtInfo.getName());
        super.saveOrUpdate(vo);
        if (isUpdate) {
            //删除公寓配套
            LambdaQueryWrapper<ApartmentFacility> apartmentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentInfoLambdaQueryWrapper.eq(ApartmentFacility::getId,vo.getId());
            apartmentFacilityService.remove(apartmentInfoLambdaQueryWrapper);
            //删除公寓标签
            LambdaQueryWrapper<ApartmentLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            labelLambdaQueryWrapper.eq(ApartmentLabel::getId,vo.getId());
            apartmentLabelService.remove(labelLambdaQueryWrapper);
            //删除公寓杂费
            LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getId,vo.getId());
            apartmentFeeValueService.remove(feeValueLambdaQueryWrapper);
            //删除公寓图片
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            LambdaQueryWrapper<GraphInfo> eq = graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT)
                    .eq(GraphInfo::getItemId, vo.getId());
            graphInfoService.remove(eq);
        }
        //更新公寓配套
        List<Long> facilityInfoIds = vo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){//防止空指针
            List<ApartmentFacility> apartmentFacilityList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility apartmentFacility = new ApartmentFacility();
                apartmentFacility.setApartmentId(vo.getId());
                apartmentFacility.setFacilityId(facilityInfoId);
                apartmentFacilityList.add(apartmentFacility);
            }
            apartmentFacilityService.saveOrUpdateBatch(apartmentFacilityList);
        }
        //更新公寓标签
        List<Long> labelIds = vo.getLabelIds();
        if(!CollectionUtils.isEmpty(labelIds)){
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = new ApartmentLabel();
                apartmentLabel.setLabelId(labelId);
                apartmentLabel.setApartmentId(vo.getId());
                apartmentLabelList.add(apartmentLabel);
            }
            apartmentLabelService.saveOrUpdateBatch(apartmentLabelList);
        }
        //更新公寓图片
        List<GraphVo> graphVoList = vo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setName(graphVo.getName());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(vo.getId());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveOrUpdateBatch(graphInfoList);
        }
        //更新公寓杂费
        List<Long> feeValueIds = vo.getFeeValueIds();
        if(!CollectionUtils.isEmpty(feeValueIds)){
            List<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
                apartmentFeeValue.setFeeValueId(feeValueId);
                apartmentFeeValue.setApartmentId(vo.getId());
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveOrUpdateBatch(apartmentFeeValueList);
        }
    }

    @Override
    public void removeApartmentById(Long id) {
        LambdaQueryWrapper<RoomInfo> roomQueryWrapper = new LambdaQueryWrapper<>();
        roomQueryWrapper.eq(RoomInfo::getApartmentId, id);
        Long count = roomInfoMapper.selectCount(roomQueryWrapper);
        if (count > 0) {
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        //1.删除GraphInfo
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphQueryWrapper);

        //2.删除ApartmentLabel
        LambdaQueryWrapper<ApartmentLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
        labelQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(labelQueryWrapper);

        //3.删除ApartmentFacility
        LambdaQueryWrapper<ApartmentFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
        facilityQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(facilityQueryWrapper);

        //4.删除ApartmentFeeValue
        LambdaQueryWrapper<ApartmentFeeValue> feeQueryWrapper = new LambdaQueryWrapper<>();
        feeQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(feeQueryWrapper);

        //5.删除ApartmentInfo
        super.removeById(id);
    }

    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageApartmentItemByQuery(page,queryVo);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        //先将公寓的信息查出来，若存在，再去查询该公寓下的其他信息
        ApartmentInfo apartmentInfo = this.getById(id);
        if(apartmentInfo==null)
            return null;
        //此处需要把公寓的相关信息全部查出来
        //1.配套信息
        List<FacilityInfo> facilityInfoList = apartmentFacilityService.selectListByApartmentId(id);
        //2.图片信息
        List<GraphVo> graphVoList = graphInfoService.selectListByItemTypeAndId(ItemType.APARTMENT, id);
        //3.杂费信息
        List<FeeValueVo> feeValueVoList = apartmentFeeValueService.selectListByApartmentId(id);
        //4.标签信息
        List<LabelInfo> labelInfoList = apartmentLabelService.selectListByApartmentId(id);

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        return apartmentDetailVo;
    }
}




